package com.alisls.demo.springcloud.service.user.schedule;

import com.alisls.demo.springcloud.service.user.client.oauth.TokenFeignClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时获取Token
 *
 * @author Ke Wang
 * @date 2020/10/31
 */
@Component
public class TokenTask {

    @Resource
    private TokenFeignClient tokenFeignClient;

    /**
     * 定时任务，从第0分钟开始，每50分钟出发一次
     * @throws URISyntaxException
     */
    @Scheduled(cron = "0 0/50 * * * ?")
    public void refreshToken() throws URISyntaxException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String authorization = "Basic ZGVtby1zcHJpbmdjbG91ZC0yLXNlcnZpY2UtdXNlcjoxMjM0NTY=";
        String grant_type = "client_credentials";
        String response = tokenFeignClient.token(authorization, grant_type);
        System.out.println(simpleDateFormat.format(date) + response);
    }

}
